package graphics.view3D;

import graphics.interfaces.ICoordinateConverter2D;
import model.Square;

public class Coord3D implements ICoordinateConverter2D {
	
	// Board corners
	// xy1 xy2
	// xy3 xy4
	private int x1;
	private int y1;
	private int x2;
	private int y2;
	private int x3;
	private int y3;
	private int x4;
	private int y4;
	
	/*
	 * Pattern Singleton
	 */
    private static Coord3D instance;
    public static Coord3D getInstance() {
        if (null == instance) {
            instance = new Coord3D();
        }
        return instance;
    }
    
    public void setGeometry(int corner1x, int corner1y,
    		int corner2x, int corner2y,
    		int corner3x, int corner3y,
    		int corner4x, int corner4y) {
    	x1 = corner1x;
    	x2 = corner2x;
    	x3 = corner3x;
    	x4 = corner4x;
    	y1 = corner1y;
    	y2 = corner2y;
    	y3 = corner3y;
    	y4 = corner4y;
    }
	 
    public int getXFromSquare(Square square){
		return square.getCol() * 60;
	}

	public int getYFromSquare(Square square){
		return square.getRow() * 60;
	}
	
	/**
     * Get the column on the board from a location on he screen
     *
     * @param X location on the screen
     * @param Y location on the screen
     * @return the column
     */
    int getColFromXY(int X, int Y) {
        double Xmin = (((double) (x3 - x1) / (double) (y3 - y1)) * (double) (Y - y3) + x3);
        double Xmax = (((double) (x4 - x2) / (double) (y4 - y2)) * (double) (Y - y4) + x4);
        int col = (int) (9.0 - ((Xmax - X) / (Xmax - Xmin)) * 8.0);
        return col;
    }

	/**
     * Get the row on the board from a location on he screen
     *
     * @param X location on the screen
     * @param Y location on the screen
     * @return the row
     */
    int getRowFromXY(int X, int Y) {
        int row = (int) (((double) (y3 - Y) / (double) (y3 - y1)) * 8.0);
        return row;
    }
}
